<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('goods:spec:add')" type="primary" @click="updateHandle('',0)">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="规格名">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" v-if="isAuth('goods:spec:modify')"  @click="updateHandle(scope.row.name,scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="specsHandle(scope.row.id)">查看规格属性</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog
      :title="updateData.id === 0 ? '新增':'修改'"
      :visible.sync="updateVisible"
      style="height: 700px"
      width="30%">
      <el-form ref="updateData" :model="updateData" label-width="80px" :rules="rules">
        <el-form-item label="规格名称" placeholder="请输入" prop="name">
          <el-input v-model="updateData.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateHandle">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="查看规格属性"
      :visible.sync="propertyVisible"
      style="height: 700px"
      width="30%">
      <el-table
        :data="propertyList"
        border
        style="width: 100%">
        <el-table-column
          prop="name"
          align="center"
          label="属性名">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="updatePropertyHandle(scope.row.name,scope.row.id)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updatePropertyHandle('',0)">新增</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :title="updatePropertyData.id === 0 ? '新增属性':'修改属性'"
      :visible.sync="updatePropertyVisible"
      style="height: 700px"
      width="30%">
      <el-form ref="updatePropertyData" :model="updatePropertyData" label-width="80px" :rules="rules">
        <el-form-item label="属性名称" placeholder="请输入" prop="name">
          <el-input v-model="updatePropertyData.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updatePropertyVisible = false">取 消</el-button>
        <el-button type="primary" @click="propertyAddOrUpdateHandle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          keywords: ''
        },
        updateData: {},
        updatePropertyVisible: false,
        updatePropertyData: {},
        dataList: [],
        ificationId: 0,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        updateVisible: false,
        propertyVisible: false,
        propertyList: [],
        rules: {
          name: [
            { required: true, message: '请输入规格名称', trigger: 'blur' }
          ]
        }
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/goods/spec/queryGoodsSpecList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': 10,
            'keywords': this.dataForm.keywords
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      updateHandle (name, id) {
        this.updateData = {
          name: name,
          id: id
        }
        this.updateVisible = true
      },
      updatePropertyHandle (name, id) {
        this.updatePropertyData = {
          name: name,
          id: id
        }
        this.updatePropertyVisible = true
      },
      specsHandle (id) {
        this.ificationId = id
        this.$http({
          url: this.$http.adornUrl(`/goods/spec/queryPropertyByIficationIdList`),
          method: 'post',
          data: this.$http.adornData(this.ificationId, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.propertyList = data.data
            this.propertyVisible = true
          }
        })
      },
      // 新增 / 修改
      addOrUpdateHandle () {
        this.$http({
          url: this.$http.adornUrl(`/goods/spec/${this.updateData.id === 0 ? `add` : `modifyById`}`),
          method: 'post',
          data: this.$http.adornData({
            'id': this.updateData.id || 0,
            'name': this.updateData.name
          }, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.updateVisible = false
            this.$message.success('操作成功')
            this.getDataList()
          } else {
            this.$message.error('操作失败')
          }
        })
      },
      // property新增 / 修改
      propertyAddOrUpdateHandle () {
        this.$http({
          url: this.$http.adornUrl(`/goods/spec/${this.updatePropertyData.id === 0 ? `addProperty` : `modifyPropertyById`}`),
          method: 'post',
          data: this.$http.adornData({
            'id': this.updatePropertyData.id || 0,
            'name': this.updatePropertyData.name,
            'specId': this.ificationId
          }, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success('操作成功')
            this.specsHandle(this.ificationId)
            this.updatePropertyVisible = false
          } else {
            this.$message.error('操作失败')
          }
        })
      }
    }
  }
</script>
